package com.bfly.mall.product.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.mall.product.dao.ProductTxtMapper;
import com.bfly.mall.product.entity.ProductTxt;
import com.bfly.mall.product.dto.ProductTxtFormDTO;
import com.bfly.mall.product.service.IProductTxtService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/21 12:32
 */
@Service
public class ProductTxtServiceImpl extends BaseServiceImpl<ProductTxt, Integer> implements IProductTxtService {

    @Autowired
    private ProductTxtMapper txtMapper;

    @Override
    public ProductTxt getTxt(int productId) {
        return txtMapper.getTxt(productId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void merge(int productId, ProductTxtFormDTO formDTO) {
        // 先清除详情 再保存
        txtMapper.clearTxt(productId);
        ProductTxt txt = new ProductTxt();
        BeanUtils.copyProperties(formDTO, txt);
        txt.setProductId(productId);
        txt.setCreateDate(new Date());
        txt.setUpdateDate(txt.getCreateDate());
        super.save(txt);
    }
}